package hut.lvming.Controller;

import hut.lvming.domain.User;
import hut.lvming.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

//    @RequestMapping("/out")
//    public String test1(){
//
//        System.out.println(111);
//        return "main";
//    }

    @RequestMapping("/login")
    public String login(User userF, HttpServletRequest request){
        User userT=userService.login(userF);
//        System.out.println(userF);
//        System.out.println(userT);
        try{
            if(userT!=null){
                request.getSession().setAttribute("USER_SESSION",userT);
                return "redirect:/admin/main.jsp";
            }
            request.setAttribute("msg","邮箱或密码错误，请重新登录！");
            return "forward:/admin/login.jsp";
        }catch (Exception e){
            e.printStackTrace();
            request.setAttribute("msg","系统错误！");
            return "forward:/admin/login.jsp";
        }
    }

    @RequestMapping("logout")
    public String logout(HttpServletRequest request){
//        System.out.println(222222222);
        try{
            HttpSession session=request.getSession();
            session.invalidate();
            return "forward:/admin/login.jsp";
        }catch (Exception e){
            e.printStackTrace();
            request.setAttribute("msg","系统错误");
            return "forward:/admin/login.jsp";
        }
    }
}
